package cn.dengta.webapp.user.entity;

import java.util.Date;
import java.util.Map;
import javax.annotation.Nullable;

import cn.dengta.common.model.*;
import cn.dengta.context.model.*;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.base.entity.I18nContent;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.*;

/**
 * 举报类型
 *
 * @author lgy
 * @since 2022-03-01
 */
@Getter
@Setter
@Accessors(chain = true)
public class ComplainType extends BizEntity {

	/** 内容 */
	private String name;
	/** 0=用户；1=动态； 2=语聊；3=视频；4=群组 */
	private BizType type;
	/** 排序 */
	private Integer sort;
	protected transient I18nContent resolvedLanguageCfg;

	public ComplainType init(ComplainType input, Date now) {
		this.name = X.expectNotNull(input.name, name);
		this.type = X.expectNotNull(input.type, type);
		this.sort = X.expectNotNull(input.sort, sort, 0);
		super.initTime(now);
		super.init(Status.YES.value, State.PUBLIC);
		return this;
	}

	@Nullable
	public I18nContent getResolvedLanguageCfg() {
		I18nContent cfg = resolvedLanguageCfg;
		if (cfg == null) {
			resolvedLanguageCfg = cfg = StringUtil.isEmpty(name) ? null : I18nContent.deserialize(name);
		}
		return X.castType(cfg);
	}

	public void updateName(I18nContent config) {
		this.name = I18nContent.serialize(this.resolvedLanguageCfg = config);
	}

	public I18nContent updateResolvedLanguageCfg(String config) {
		this.resolvedLanguageCfg = I18nContent.deserialize(this.name = config);
		return getResolvedLanguageCfg();
	}

	public String getI18nName() {
		return X.map(getResolvedLanguageCfg(), I18nContent::get);
	}

	public String getI18nName(Language language) {
		return X.map(getResolvedLanguageCfg(), l -> l.get(language));
	}

	public void setId(Long id) {
		this.id = id;
	}

	public static Map<String, Object> asEmbed(ComplainType source) {
		return asEmbed(source, t -> CollectionUtil.ofHashMap("id", t.getId(), "name", t.getI18nName(),
				"type", X.map(t.getType(), BizType::getValue), "type_", X.map(t.getType(), BizType::getLabel)));
	}

	@Getter
	public enum BizType implements LabelI18nProxy<BizType, Integer> {
		/** 用户 */
		User(0, UserEnumI18nKey.BIZ_TYPE_USER, 1),
		/** 动态 */
		Moment(1, UserEnumI18nKey.BIZ_TYPE_MOMENT, 1),
		/** 语聊 */
		WORD(2, UserEnumI18nKey.BIZ_TYPE_WORD_CHAT, 1),
		/** 视频 */
		VIDEO(3, UserEnumI18nKey.BIZ_TYPE_VIDEO_CHAT, 1),
		/** 群 */
		GROUP(4, UserEnumI18nKey.BIZ_TYPE_GROUP_CHAT, 1),
		AI_FEEDBACK(5, UserEnumI18nKey.BIZ_TYPE_AI_FEEDBACK, 1),
		AI(6, UserEnumI18nKey.BIZ_TYPE_AI, 1),
		/** 充值 */
		RECHARGE(7, UserEnumI18nKey.BIZ_TYPE_RECHARGE, 1),
		//
		;

		// 定义私有变量
		public final Integer value;
		// 每天限制举报同对象添加次数
		public final Integer perLimit;
		final ValueProxy<BizType, Integer> proxy;

		// 构造函数，枚举类型只能为私有
		BizType(Integer value, String label, Integer perLimit) {
			this.value = value;
			this.perLimit = perLimit;
			this.proxy = new ValueProxy<>(this, value, label);
		}

		public static BizType[] getValues() {
			return ValueProxy.getCachedArray(BizType.class, BizType::values);
		}

		public static BizType of(Integer value) {
			return User.getValueOf(value);
		}


		public boolean isAi(){
			return ArrayUtil.ins(this, AI_FEEDBACK, AI);
		}
	}

	public static final String NAME = "name";
	public static final String TYPE = "type";
	public static final String SORT = "sort";

}
